<?php 

if( !defined('CORE_PATH')) die('Access Denied');

/***
 * Provides an implementation of the base Manager class for the purpose of managing
 * the quick_links database table.
 */
class LinksModule extends Module {
    
    protected $models = array('Link','LinkCategory');

    /**
     * Constructs the LinksManager class and generates the initial number of records
     * to be loaded from the database
     *
     * @param int $total the total number of records to generate
     */
    function __construct() {
		parent::__construct( dirname(__FILE__) );
    }
	
	public function index() {
		$categories = $this->getLinkCategories();
		
		$this->set('categories', $categories);	
	}
	
	public function list_links() {
		$this->autoLayout = false;
		
		// get the number of records to be returned and the position to start returning
	    // them from limit for the number of records to returned
	    $limit = isset($_POST['limit']) ? $_POST['limit'] : 20;
	    $start = isset($_POST['start']) ? $_POST['start'] * $limit : 0;
	   
	   	// get the category to which the articles belong
	   	$cat = $_POST['category'];
	   	$category = $this->LinkCategory->findFirst(array(
	   		'filter' => array('id' => $cat)
		));
	   
	    // retrieve all the links in the specified category
	    $links = $category->getLinks($start, $limit, false);
	   
	    // get the total number of articles in this category
	    $total_links = $category->getLinksCount(false);	
	   
	    $this->set(compact('links','total_links', 'start'));
	}
    
	public function add() {
		if( !defined("IN_CMS") || !IN_CMS ) return;
		
		if( isset($_POST['LinkCategory']) ) {
			$link = new Link(array(
				'LinkCategory' => $_POST['LinkCategory'],
				'LinkName' => $_POST['LinkName'],
				'LinkDesc' => $_POST['LinkDesc'],
				'LinkURL' => $_POST['LinkURL'],
				'LinkTarget' => $_POST['LinkPosition'],
				'Published' => isset($_POST['Published']) ? 'true' : 'false'
			));		
			$link->save();
			
			$this->redirect(array('action' => 'index'));
		}
		
		
		$categories = $this->LinkCategory->find(array(
			'filter' => array('SiteID' => SITE_ID)
		));
		
		$this->set('categories', $categories);
		
	}
	
	public function edit($id) {
		
		if( isset($_POST['LinkCategory']) ) {
			
			$link = new Link(array(
				'LinkCategory' => $_POST['LinkCategory'],
				'LinkName' => $_POST['LinkName'],
				'LinkDesc' => $_POST['LinkDesc'],
				'LinkURL' => $_POST['LinkURL'],
				'LinkTarget' => $_POST['LinkPosition'],
				'Published' => isset($_POST['Published']) ? 'true' : 'false',
				'id' => $_POST['id']
			));		
			$link->save();	
			
			$this->redirect(array('action' => 'index'));
		}
		
		$link = $this->Link->findFirst(array(
			'filter' => array('id' => $id)
		));	
		
		$categories = $this->LinkCategory->find(array(
			'filter' => array('SiteID' => SITE_ID)
		));
		
		$this->set('link', $link);
		$this->set('categories', $categories);
	}
	
	public function delete($id) {
		$this->autoRender = false;	
		
		if( isset($_POST['link_ids']) ) {
			$this->Link->deleteAll(array('id IN' => explode(",", $_POST['link_ids'])));
		}
		
		else if( intval($id) > 0 ) {
			$this->Link->setId($id)->delete();
		}
	}
	
	public function move_links() {
		$this->autoRender = false;
		
		if( $this->Request->isAjax() ) {	
			$category_id = $_POST['category_id'];
			$msg_ids = $_POST['msg_ids'];
			$this->Link->updateAll(array('LinkCategory' => $category_id), array('id IN' => explode(",", $msg_ids)));	
		}
	}
	
	public function add_category() {
		$this->autoRender = false;
		
		if( !defined("IN_CMS") || !IN_CMS ) return;
		
		$linkCat = new LinkCategory(array(
			'SiteID' => SITE_ID,
			'CategoryName' => $_POST['CategoryName'],
			'CategoryDescription' => $_POST['CategoryDesc']
		));
		
		$linkCat->save();
		
		echo $linkCat->toJSON();
	}
	
	public function delete_category() {
		$this->autoRender = false;
		
		$this->LinkCategory->setId($_POST['category_id'])->delete();	
	}
	
	public function publish_link() {
		$this->autoRender = false;
		
	    $this->Link->publish($_POST['id'], $_POST['action'] == "publish");	
	}
	
    /**
     * Returns a link from the database
     * 
     * @param mixed $id
     * @return Link
     */
    public function getLink($id) {
        return $this->Link->findFirst(array(
            'filter' => array(
                'OR' => array(
                    'id' => $id
                )
            )
        ));
    }
	
	public function getLinks($start = 0, $limit = NULL) {
        
        return $this->Link->paginate($start, $limit, array(
            'joins' => array(
                array("type" => "inner", "condition" => array("quick_links_categories qlc" => "qlc.id = Link.LinkCategory"))
            ),
            'filter' => array("SiteID" => SITE_ID, "Published" => 'true'),
            "order" => array("LinkCategory, LinkPosition, LinkName")
        ));
    }

    /**
     * Returns an Array of LinkCategory objects containing the data for category objects
     * 
     * @param int $siteid the database record ID of the site to load the categories for
     * @return array an Array of LinkCategory objects
     */
	public function getLinkCategories() {
        return $this->LinkCategory->find( array(
            'filter' => array(
                'SiteID' => SITE_ID
            ),
            'order' => array('CategoryName' => 'ASC')
        ));
	}
	
	public function updateCategory() {
		if( !defined("IN_CMS") || IN_CMS ) return;
		
		$linkCat = new LinkCategory(array(
			'SiteID' => SITE_ID,
			'CategoryName' => $_POST['CategoryName'],
			'CategoryDescription' => $_POST['CatDesc'],
			'id' => $_POST['EditCatID']
		));
		
		$linkCat->save();
	}
}








?>